<template>
  <el-dialog v-model="state.isFalse" :title="'联动 - ' + state.form.alarm_name" :before-close="onVisable" :draggable="true" :width="850">
    <div class="ww100 flex-bc mb20 white">
      <div class="flex-sc"></div>
      <div class="flex-ec flex1 h30">
        <div class="rad4 ptb6 plr10 flex-cc cursor bgi3 white ml10" 
          v-if="state.content.find(v=>{return typeof(v.key) == 'object'}) && state.content.find(v=>{return typeof(v.key) == 'object'})['key'].hasOwnProperty('add')" 
          @click.stop="handleClick('add', state.addItem)">
          <i-ep-plus class="f12 fw" /><span class="f14 ml5">{{state.content.find(v=>{return typeof(v.key) == 'object'})['key'].add}}</span>
        </div>
      </div>
    </div>
    <div class="row bgi22 white">
      <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
    </div>
    <div class="table white">
      <div class="row" :class="publicStore.active[state.key] == item[state.key]?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
        <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
          <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
           <span v-else-if="v.key=='*'">{{index+1}}</span>
          <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
          <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
            {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
          </span>
          <span v-else-if="v.type == 'switch'">
            <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
          </span>
          <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
          <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
        </div>
      </div>
      <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
    </div>
    <Add @init="init" @changeSelect="changeSelect" :state="state" ref="exposeRef" />
  </el-dialog>
</template>

<script lang="ts" setup>
  import Add from '@/components/Add'
  const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  const state = reactive({
	  ...publicStore.$state,
    key: 'id',
    content: [
      { width: 'w50x5', show: true, align: 'left', key: 'sensor_id',       name: '联动传感器', type: 'find', value: [[], 'id', 'name'] }, 
      { width: 'w50x4', show: true, align: 'left', key: 'sensor_ctrl_pro', name: '控制点位', type: 'find', value: [[], 'code', 'name'] },
      { width: 'w50x4', show: true, align: 'left', key: 'ctrl_code',       name: '控制动作码'}, 
      { width: 'flex1', show: true, align: 'right', key: {add: '新增', edit:'编辑', del: '删除'},  name: '操作' },
    ],
    editFrom: [
      { required: true, editshow: true,  name: '联动传感器', key: 'sensor_id', type: ['tree', [], 'id', 'parent_id'], relation: true },
      { required: true, editshow: true,  name: '控制点位', key: 'sensor_ctrl_pro', type: ['select', [], 'code', 'name'] },
      { required: true,  editshow: true, name: '控制动作码',   key: 'ctrl_code', type: ['input'] }, 
    ],
    addItem: {},
  })
  const props = defineProps({
    state: {
      type: [Object, Array],
      default: ()=>{return {}}
    },
  })
  let exposeRef = $ref()


  onMounted(async() => {
    getInit()
  })

  const getInit = async(key) => {
    state.query = {"o": "o_sensor_template_control_property", "i": 0}
    state.params = {queryApi: state.query}
    let res = await publicStore.post(state.params)
    state.list = proxy.isNull(res)? [] : res
  }

  // 打开关闭弹窗
  const onVisable = async (val, mark) => {
    state.isFalse = !state.isFalse
    if(!state.isFalse) return
    state.form = Object.assign({}, val)
    state.addItem = {alert_id: val.id}
    let i = mark == 'o_sensor_alert_interval'? 1 : mark == 'o_sensor_alert_state'? 0 : ''
    state.query = {"o": mark + '_action', "p":[val.id], "i": i}
    setInit()
    init()
  }

  const setInit = () => {
    state.content.forEach(v => {
      if(v.key == 'sensor_id') v.value[0] = publicStore.list
      if(v.key == 'sensor_ctrl_pro') v.value[0] = state.list
    })
    state.editFrom.forEach(v => {
      if(v.key == 'sensor_id') v.type[1] = publicStore.list
      if(v.key == 'sensor_ctrl_pro') v.type[1] = state.list
    })
  }

  const init = async() => {
    state.params = {queryApi: state.query}
    let res = await publicStore.post(state.params)
    state.list = proxy.isNull(res)? [] : res
    state.empty = proxy.isNull(res)? true : false
  }

  const handleClick = (remark, val) => {
    if(remark == 'add'){
      exposeRef.onVisable(val)
    }
    if(remark == 'edit'){
      let newval = {id: val.id, alert_id: val.alert_id, sensor_ctrl_pro: val.sensor_ctrl_pro, ctrl_code: val.ctrl_code, sensor_id: val.sensor_id}
      exposeRef.onVisable(newval)
    }
    if(remark == 'del'){
      exposeRef.del(val, state.query, state.key)
    }
  }

  const changeSelect = async(val) => {
    let type = proxy.find(publicStore.list, ['id', val], 'type')
    setInit()
  }

  // 暴露
  defineExpose({
    onVisable
  })
</script>

<style scoped lang="scss">
:deep(.el-dialog__body){
  color: #ffffff !important;
}
</style>